package com.nuoniu.sibanyun.service.crm.call.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallAgentStatistics;
import com.nuoniu.sibanyun.entity.crm.call.dto.CrmCallAgentStatisticsSearchDto;
import com.nuoniu.sibanyun.mapper.crm.call.CrmCallAgentStatisticsMapper;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallAgentStatisticsService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 客户管理-坐席统计 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2022-04-28
 */
@Service
public class CrmCallAgentStatisticsServiceImpl extends ServiceImpl<CrmCallAgentStatisticsMapper, CrmCallAgentStatistics> implements ICrmCallAgentStatisticsService {

    @Override
    public Page<CrmCallAgentStatistics> pageAgentStatisticsBySystem(CrmCallAgentStatisticsSearchDto crmCallAgentStatisticsSearchDto) {
        QueryWrapper<CrmCallAgentStatistics> queryWrapper = new QueryWrapper<>();
        Integer companyId = crmCallAgentStatisticsSearchDto.getCompanyId();
        if(null != companyId){
            queryWrapper.eq("company_id",companyId);
        }
        Page page = page(crmCallAgentStatisticsSearchDto.getPage(), queryWrapper);
        return page;
    }

    @Override
    public CrmCallAgentStatistics getByCompanyId(Integer companyId) {
        QueryWrapper<CrmCallAgentStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        queryWrapper.last("LIMIT 1");
        CrmCallAgentStatistics agentStatistics = getOne(queryWrapper, false);
        return agentStatistics;
    }

    @Override
    public CrmCallAgentStatistics getCrmCallAgentStatistics() {
        UserInfo userInfo = UserThreadLocal.get();
        Integer companyId = userInfo.getCompanyId();
        QueryWrapper<CrmCallAgentStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        queryWrapper.last("LIMIT 1");
        CrmCallAgentStatistics agentStatistics = getOne(queryWrapper, false);
        /** 校验统计是否需要重新 统计*/
        try {
            if(null != agentStatistics && agentStatistics.getIsNeedCheck()){
                this.checkAgentStatistics(companyId);
            }
        }catch (Exception e){

        }
        return agentStatistics;
    }

    @Override
    public void checkFromAddAgent(Integer newAgentNumber) {
        UserInfo userInfo = UserThreadLocal.get();
        CrmCallAgentStatistics statistics = this.getCrmCallAgentStatistics();
        if(null == statistics){
            statistics = new CrmCallAgentStatistics();
            statistics.setCompanyId(userInfo.getCompanyId());
            statistics.setCompanyName(userInfo.getCompanyName());
            statistics.setCreateDate(LocalDateTime.now());
            statistics.setCreateUserId(userInfo.getUserId());
            statistics.setCreateUserName(userInfo.getRealName());
            statistics.setNewNumber(newAgentNumber);
            statistics.setTotalNumber(0);
            statistics.setEnableNumber(0);
            statistics.setDisableNumber(0);
            save(statistics);
        }else{
            CrmCallAgentStatistics updateCrmCallAgentStatistics = new CrmCallAgentStatistics();
            updateCrmCallAgentStatistics.setId(statistics.getId());
            updateCrmCallAgentStatistics.setNewNumber(statistics.getNewNumber() + newAgentNumber);
           updateById(updateCrmCallAgentStatistics);
        }

    }

    @Override
    public void batchUpdateCompanyList(Set<Integer> companyIds) {
        if(companyIds.size() == 0)return;
        UpdateWrapper<CrmCallAgentStatistics> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("company_id",companyIds);
        updateWrapper.set("is_need_check",1);
        this.update(updateWrapper);
    }

    /**
     * 更新  坐席数量 统计
     * @param companyId
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void checkAgentStatistics(Integer companyId) {
        baseMapper.updateAgentStatistics(companyId);
        UpdateWrapper<CrmCallAgentStatistics> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("company_id",companyId);
        updateWrapper.set("is_need_check",0);
        this.update(updateWrapper);
    }
}
